/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FrmUserView.java
 *
 * Created on 22 Sep 12, 12:15:57
 */
package sistemperpustakaan.view.user;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.event.InternalFrameEvent;
import javax.swing.event.InternalFrameListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import sistemperpustakaan.controller.UserController;
import sistemperpustakaan.entity.ViewUser;
import sistemperpustakaan.model.UserModel;
import sistemperpustakaan.tablemodel.UserTableModel;
import sistemperpustakaan.view.FrmMain;

/**
 *
 * @author EKo
 */
public class FrmUserView extends javax.swing.JInternalFrame implements InternalFrameListener, ListSelectionListener {

    FrmMain frameMain;
    DialogDetailUser detailUser;
    UserController controller;
    UserModel model;
    UserTableModel tableModel;
    String idUser = "";

    /** Creates new form FrmUserView */
    public FrmUserView(FrmMain main) {
        this.frameMain = main;
        controller = new UserController(this);
        model = new UserModel();
        controller.setModel(model);
        initComponents();
        addInternalFrameListener(this);
        tableUser.getSelectionModel().addListSelectionListener(this);
        controller.fillComboType();
        tableModel = new UserTableModel(new ArrayList<ViewUser>());
        tableUser.setModel(tableModel);
        setTableWidth();
        controller.findListUser();
    }

    public UserModel getModel() {
        return model;
    }

    public void setModel(UserModel model) {
        this.model = model;
    }

    public JTable getTableUser() {
        return tableUser;
    }

    public void setTableUser(JTable tableUser) {
        this.tableUser = tableUser;
    }

    public UserController getController() {
        return controller;
    }

    public void setController(UserController controller) {
        this.controller = controller;
    }

    public FrmMain getFrameMain() {
        return frameMain;
    }

    public void setFrameMain(FrmMain frameMain) {
        this.frameMain = frameMain;
    }

    public UserTableModel getTableModel() {
        return tableModel;
    }

    public void setTableModel(UserTableModel tableModel) {
        this.tableModel = tableModel;
    }

    public String getIdUser() {
        return idUser;
    }

    public void setIdUser(String idUser) {
        this.idUser = idUser;
    }

    public JComboBox getComboType() {
        return ComboType;
    }

    public void setComboType(JComboBox ComboType) {
        this.ComboType = ComboType;
    }

    public JTextField getTxtNama() {
        return txtNama;
    }

    public void setTxtNama(JTextField txtNama) {
        this.txtNama = txtNama;
    }

    public JTextField getTxtUserId() {
        return txtUserId;
    }

    public void setTxtUserId(JTextField txtUserId) {
        this.txtUserId = txtUserId;
    }

    public JLabel getLblJumlah() {
        return lblJumlah;
    }

    public void setLblJumlah(JLabel lblJumlah) {
        this.lblJumlah = lblJumlah;
    }

    public void setTableWidth() {
        int[] w = {100, 120, 200, 350};
        for (int i = 0; i < 4; i++) {
            tableUser.getColumnModel().getColumn(i).setPreferredWidth(w[i]);
        }
    }

    public void setAction() {
        frameMain.getBtnCari().setActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if (isSelected()) {
                    controller.findListUser();
                }
            }
        });

        frameMain.getBtnRefresh().setActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if (isSelected()) {
                    controller.refreshListUser();
                }
            }
        });

        frameMain.getBtnDetail().setActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if (idUser =="") {
                    JOptionPane.showMessageDialog(rootPane, "Anda belum memilih data");

                } else if(isSelected()){
                    detailUser = new DialogDetailUser(frameMain, true, controller, idUser);
                    detailUser.setVisible(true);
                }
            }
        });

        frameMain.getBtnTambah().setActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if (isSelected()) {
                    detailUser = new DialogDetailUser(frameMain, true, controller);
                    detailUser.setVisible(true);
                }
            }
        });

        frameMain.getBtnHapus().setActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if (idUser =="") {
                    JOptionPane.showMessageDialog(rootPane, "Anda belum memilih data");
                } else if(isSelected()){
                    controller.saveDeleteUser();                }
            }
        });

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        gbr = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtUserId = new javax.swing.JTextField();
        txtNama = new javax.swing.JTextField();
        ComboType = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableUser = new javax.swing.JTable();
        jLabel4 = new javax.swing.JLabel();
        lblJumlah = new javax.swing.JLabel();

        setClosable(true);
        setIconifiable(true);
        setTitle("Data User");

        gbr.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        gbr.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sistemperpustakaan/image/user.png"))); // NOI18N

        jLabel11.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel11.setText("Data User");

        jLabel1.setText("User ID");

        jLabel2.setText("Nama User");

        jLabel3.setText("Tipe User");

        ComboType.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        tableUser.setAutoCreateRowSorter(true);
        tableUser.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tableUser.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        jScrollPane1.setViewportView(tableUser);

        jLabel4.setText("item(s) found");

        lblJumlah.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        lblJumlah.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblJumlah.setText("0");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 619, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3))
                        .addGap(50, 50, 50)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtNama, javax.swing.GroupLayout.DEFAULT_SIZE, 199, Short.MAX_VALUE)
                            .addComponent(txtUserId, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(ComboType, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 199, Short.MAX_VALUE)
                        .addComponent(lblJumlah, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel11))))
                .addContainerGap())
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(373, 373, 373)
                    .addComponent(gbr, javax.swing.GroupLayout.DEFAULT_SIZE, 256, Short.MAX_VALUE)
                    .addContainerGap()))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(27, 27, 27)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(txtUserId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(txtNama, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(ComboType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4)
                            .addComponent(lblJumlah)))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel11)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 295, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(gbr)
                    .addContainerGap(158, Short.MAX_VALUE)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox ComboType;
    private javax.swing.JLabel gbr;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblJumlah;
    private javax.swing.JTable tableUser;
    private javax.swing.JTextField txtNama;
    private javax.swing.JTextField txtUserId;
    // End of variables declaration//GEN-END:variables

    public void internalFrameOpened(InternalFrameEvent e) {
    }

    public void internalFrameClosing(InternalFrameEvent e) {
    }

    public void internalFrameClosed(InternalFrameEvent e) {
        //frameMain.getDesktopPane().selectFrame(true);
    }

    public void internalFrameIconified(InternalFrameEvent e) {
    }

    public void internalFrameDeiconified(InternalFrameEvent e) {
    }

    public void internalFrameActivated(InternalFrameEvent e) {
        setAction();
    }

    public void internalFrameDeactivated(InternalFrameEvent e) {
    }

    public void valueChanged(ListSelectionEvent e) {
        try {
            idUser = (String)(tableUser.getValueAt(tableUser.getSelectedRow(), 0).toString());
        } catch (Exception ex) {
        }
    }
}
